package com.restaurantonlineorder.oauth.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.restaurantonlineorder.response.BaseResult;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 熊宇
 * @date 2022/7/20 22:46
 * @description 响应工具类
 */
@Slf4j
public class ResponseUtil {

    private static final String CONTENT_TYPE = "application/json;charset=utf-8";

    private ResponseUtil() {
    }

    public static void sendMessage(HttpServletResponse response, BaseResult<?> baseResult) {
        response.setContentType(CONTENT_TYPE);
        response.setStatus(HttpServletResponse.SC_OK);
        try (PrintWriter out = response.getWriter()) {
            out.write(new ObjectMapper().writeValueAsString(baseResult));
            out.flush();
        } catch (IOException e) {
            log.error("使用ResponseUtil发送消息失败:{}", e.getMessage(), e);
        }
    }

}
